
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 对技能特效图层归类：6个技能，每个技能分配单独1层，后续技能数量上限增加，则sortlayer也必须增加，避免大批量技能触发打断批处理，降低DrawCall
    /// </summary>
    public class SkillSortingLayer : MonoBehaviour
    {
        [Header("控制在哪个Camera图层下渲染")]
        [SerializeField]
        [Layer]
        private int layer;

        [Header("控制在哪个图层下批处理")]
        [SerializeField]
        [SortLayer]
        private int sortlayer;

        [Header("控制在图层层级")]
        [SerializeField]
        private int orderId = 0;

        [Header("是否需要自动调节OrderId顺序")]
        [SerializeField]
        private bool isSetOrderId = true;
        //记录初始order
        private struct RenderSortData
        {
            public Renderer render;
            public int order;
        }

        private List<RenderSortData> allRenders = new List<RenderSortData>();

        //public int Layer
        //{
        //    get { return layer; }
        //    set { layer = value; ChangeChildLayer(); }
        //}

        //public int SortingLayer
        //{
        //    get
        //    {
        //        return this.sortlayer;
        //    }
        //    set
        //    {
        //        this.sortlayer = value;
        //        this.SortLayer();
        //    }
        //}


        private void Awake()
        {
            FindRenderer();
        }

        private void Start()
        {
            ChangeChildLayer();
            Sort();
        }
        /// <summary>
        /// 分配技能渲染图层
        /// </summary>
        /// <param name="layer">控制在哪个Camera图层下渲染</param>
        /// <param name="sortlayer">控制在哪个图层下批处理</param>
        /// <param name="isSetOrderId">是否需要自动调节OrderId顺序</param>
        public void Init(int layer, int sortlayer, int orderId = 0, bool isSetOrderId = true)
        {
            this.layer = layer;
            this.orderId = orderId;
            this.isSetOrderId = isSetOrderId;
            //if (this.layer == LayerManager.EffectBottom)
            //{
            //    this.sortlayer = LayerManager.GetEffectButtomSortLayer(sortlayer);
            //}
            //else
            {
                this.sortlayer = sortlayer;
            }
            ChangeChildLayer();
        }

        public void FindRenderer()
        {
            allRenders.Clear();

            var _renders = GetComponentsInChildren<Renderer>(true);
            foreach (Renderer renderer in _renders)
            {
                allRenders.Add(new RenderSortData() { order = renderer.sortingOrder, render = renderer });
            }

            int orderIndex = 0;
            allRenders.Sort((a, b) => { return a.order.CompareTo(b.order); }); //排序层级高的在最后
            for (int i = 0; i < allRenders.Count; i++)
            {
                var t = allRenders[i];
                t.order = t.order + orderIndex++;//分配新的order
                allRenders[i] = t;
            }

        }

        public void ChangeChildLayer()
        {
            gameObject.ChangeChildLayer(layer);
        }

        private void Sort()
        {
            if (this.allRenders == null || allRenders.Count == 0)
            {
                return;
            }

            foreach (var p in allRenders)
            {
                if (p.render == null) continue;
                //if(p.render is MeshRenderer)
                //{
                //    //关闭特效的设置，避免报错
                //    var setrender = p.render.GetComponent<SetRendererSortingLayer>();
                //    if (setrender != null) setrender.enabled = false;
                //}
                p.render.sortingLayerID = this.sortlayer;
                if (isSetOrderId) p.render.sortingOrder = p.order + this.orderId;
            }
        }



        private void OnDestroy()
        {
            allRenders.Clear();
            allRenders = null;
        }

    }
}